package com.bw.zg3_day03lx.user

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.mvvm.BaseActivity
import com.bw.base.net.Const
import com.bw.zg3_day03lx.R
import com.bw.zg3_day03lx.databinding.ActivityLoginBinding
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/app/LoginActivity")
@AndroidEntryPoint
class LoginActivity : BaseActivity<ActivityLoginBinding,UserViewModel>() {
    override fun initView() {
        binding.btnLogin.setOnClickListener {
            val uname = binding.etUsername.text.toString()
            val pwd = binding.etPassword.text.toString()
            if (uname.isNullOrEmpty() || pwd.isNullOrEmpty()){
                ToastUtils.showLong("请先输入内容")
                return@setOnClickListener
            }
            viewModel.login(uname,pwd)
        }
    }

    override fun initData() {
        viewModel.loginData.observe(this){
            ARouter.getInstance().build("/app/MainActivity").navigation()
            SPUtils.getInstance().apply {
                put(Const.TOKEN,it.token)
                put(Const.UID,it.id)
                put(Const.USERNAME, it.username)
                put(Const.ROLE, it.roleid)
            }
        }

    }

}